package com.payment.ktb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.main4.ShowPhotoActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsPay;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.DeviceUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/6/2.
 */
public class LoginActivityOld extends BaseActivity {


    @BindView(R.id.et_loginactivity_username)
    EditText et_loginactivity_username;
    @BindView(R.id.et_loginactivity_password)
    EditText et_loginactivity_password;
    @BindView(R.id.cb_loginactivity_savepassword)
    CheckBox cb_loginactivity_savepassword;
    @BindView(R.id.iv_login_qrcode)
    ImageView iv_login_qrcode;


    @BindView(R.id.iv_login_pass)
    ImageView iv_login_pass;
    boolean passIsHide = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loginold);
        ButterKnife.bind(this);
        initData();
    }

    /**
     *
      */
    private void initData() {
        et_loginactivity_username.setText(SharedPreferencesUtils.getString("userName",""));
        et_loginactivity_password.setText(SharedPreferencesUtils.getString("password",""));
        cb_loginactivity_savepassword.setChecked(SharedPreferencesUtils.getBoolean("save",true));
    }


    @OnClick({R.id.btn_loginactivity_login,R.id.tv_loginactivity_forgetpassword,R.id.tv_loginactivity_register,R.id.ll_login_eyes,R.id.iv_login_qrcode})
    void click(View view){

        switch (view.getId()) {

            case R.id.btn_loginactivity_login:

                if (cb_loginactivity_savepassword.isChecked()) {
                    SharedPreferencesUtils.putString("password", et_loginactivity_password.getText().toString().trim());
                } else {
                    SharedPreferencesUtils.putString("password", "");
                }

                if (TextUtils.isEmpty(et_loginactivity_username.getText().toString())) {
                    ToastUtils.showToast("请输入手机号码");
                }else if (et_loginactivity_username.getText().length() < 11) {
                    ToastUtils.showToast("手机号码不正确");
                }else if (TextUtils.isEmpty(et_loginactivity_password.getText().toString())) {
                    ToastUtils.showToast("请输入密码");
                }else {
                    SharedPreferencesUtils.putString("userName",et_loginactivity_username.getText().toString().trim());
                    SharedPreferencesUtils.putBoolean("save",cb_loginactivity_savepassword.isChecked());
                    login();
                }

                break;

            case R.id.tv_loginactivity_forgetpassword:

                startActivity(ForgetPasswordActivity.class);

                break;
            case R.id.tv_loginactivity_register:

                startActivity(RegisterActivity.class);

                break;





            //
            case R.id.ll_login_eyes:
                if (passIsHide) {
                    et_loginactivity_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    iv_login_pass.setImageResource(R.mipmap.password_show);
                    passIsHide = false;
                } else {
                    et_loginactivity_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                    iv_login_pass.setImageResource(R.mipmap.password_hide);
                    passIsHide = true;
                }
                break;




            //二维码下载
            case R.id.iv_login_qrcode:

                Intent intent = new Intent(mContext,ShowPhotoActivity.class);
                intent.putExtra("qrcode","qrcode");
                startActivity(intent);

                break;


        }

    }

    /**
     * 登录
     */
    private void login() {

        progressDialogUtils.showProgress();
        //构造传输参数

        final String phone = et_loginactivity_username.getText().toString().trim();
        String pass = et_loginactivity_password.getText().toString().trim();
        String os = "ANDROID";
        String soft = "KTB";
        String imsi = DeviceUtils.getDeviceId();
        String version = DeviceUtils.getVersionName();


        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();

        map.put("phone",phone);
        map.put("pass",pass);
        map.put("os",os);
        map.put("soft",soft);
        map.put("imsi",imsi);
        map.put("version",version);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserLoginState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {
                        SharedPreferencesUtils.putString(ConstantsUser.userToken,jsonObject.optString("userToken"));
                        SharedPreferencesUtils.putString(ConstantsUser.userKey,jsonObject.optString("userKey"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardOrderMin,jsonObject.optString("nocardOrderMin"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardOrderMax,jsonObject.optString("nocardOrderMax"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardOrderEnd,jsonObject.optString("nocardOrderEnd"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardOrderStart,jsonObject.optString("nocardOrderStart"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardMcgMin,jsonObject.optString("nocardMcgMin"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardMcgMax,jsonObject.optString("nocardMcgMax"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardMcgStart,jsonObject.optString("nocardMcgStart"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardMcgEnd,jsonObject.optString("nocardMcgEnd"));
                        SharedPreferencesUtils.putString(ConstantsPay.nocardMcgEnd,jsonObject.optString("nocardMcgEnd"));
                        SharedPreferencesUtils.putString(ConstantsPay.T0,jsonObject.optString("T0"));
                        SharedPreferencesUtils.putString(ConstantsPay.T0,jsonObject.optString("T0"));
                        SharedPreferencesUtils.putString(ConstantsPay.shopName,jsonObject.optString("shopName"));
                        SharedPreferencesUtils.putString(ConstantsPay.noticetimes,"0");
                        ConstantsUser.userName = et_loginactivity_username.getText().toString().trim();
                        SharedPreferencesUtils.putString(ConstantsUser.phone,phone);
                        startActivity(MainActivity.class);
                        SharedPreferencesUtils.putString("logintime","上次登陆时间：" + new SimpleDateFormat("yyyy-MM-dd   HH:mm:ss").format(new Date()));
                    } else {


                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));

            }
        });



    }


}
